﻿using System.Collections.Generic;
using System.Data;

namespace Ledict.ConditionExtract.BLL
{
    public class StaticCacheBLL
    {
        public static Dictionary<string, DataTable> plotchildDict = new Dictionary<string, DataTable>(); //节点信息

        public static Dictionary<string, DataTable> RelationStiuationDict = new Dictionary<string, DataTable>();
        //节点相关的所有父子

        public static Dictionary<string, DataTable> AllBelowStiuationDict = new Dictionary<string, DataTable>();
        //节点相关标签

        public static Dictionary<string, DataTable> ReasonLabelDict = new Dictionary<string, DataTable>(); //节点相关标签
        public static Dictionary<string, DataTable> WordBankWordDict = new Dictionary<string, DataTable>(); //词库
        public static Dictionary<string, DataTable> WordBankWordTable = new Dictionary<string, DataTable>();
        protected ProcessDataSourceBLL datas = new ProcessDataSourceBLL();
        private int order;
        protected XmlLabel_LabelRelBLL xmlLabel_LabelRelBLL = new XmlLabel_LabelRelBLL();

        /// <summary>
        /// 得到案由的所有下级
        /// </summary>
        /// <param name="headwordid">案由ID</param>
        public DataTable GetHeadwordData(string headwordid, string parentid = ""/*2017-07-10 ljj 修改*/)
        {
            DataTable conditiondt; //下级

            lock (plotchildDict)
            {
                if (plotchildDict.ContainsKey(headwordid))
                {
                    conditiondt = plotchildDict[headwordid];
                }
                else
                {
                    order = 0;
                    conditiondt = datas.GetCauseDataSource(parentid);
                    GetHeadwordDataAndOrder(headwordid, conditiondt);
                    plotchildDict.Add(headwordid, conditiondt);
                }
            }
            return conditiondt;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="headwordid"></param>
        /// <param name="dt"></param>
        public void GetHeadwordDataAndOrder(string headwordid, DataTable dt)
        {
            var conditiondt = datas.GetCauseDataSource(headwordid);
            conditiondt.Columns.Add("TotalOrder", typeof(int));
            for (var i = 0; i < conditiondt.Rows.Count; i++)
            {
                conditiondt.Rows[i][conditiondt.Columns.Count - 1] = order++;
                dt.ImportRow(conditiondt.Rows[i]);
                GetHeadwordDataAndOrder(conditiondt.Rows[i]["CC_ID"].ToString(), dt);
            }
        }
        /// <summary>
        /// 获取给定节点的信息(民事使用)
        /// </summary>
        /// <param name="headwordid"></param>
        /// <param name="purposeId"></param>
        /// <param name="paragraphId"></param>
        /// <param name="isCurrentId"></param>
        /// <returns></returns>
        public DataTable GetCurrentCivilHeadwordData(string headwordid, string purposeId, string paragraphId, bool isCurrentId = true)
        {
            return datas.GetCauseDataSource(headwordid, purposeId, paragraphId, isCurrentId);
        }
        /// <summary>
        /// 得到给定节点的所有下级(民事使用)
        /// </summary>
        /// <param name="headwordid">节点ID</param>
        /// <param name="purposeId">用途ID</param>
        /// <param name="paragraphId">逻辑段ID</param>
        public DataTable GetCivilHeadwordData(string headwordid, string purposeId, string paragraphId)
        {
            DataTable conditiondt; //下级

            lock (this)
            {
                if (plotchildDict.ContainsKey(headwordid))
                {
                    conditiondt = plotchildDict[headwordid];
                }
                else
                {
                    order = 0;
                    conditiondt = datas.GetCauseDataSource("");
                    GetCivilHeadwordDataAndOrder(headwordid, purposeId, paragraphId, conditiondt);
                    plotchildDict.Add(headwordid, conditiondt);
                }
            }
            return conditiondt;
        }
        public void GetCivilHeadwordDataAndOrder(string headwordid, string purposeId, string paragraphId, DataTable dt)
        {
            var conditiondt = datas.GetCauseDataSource(headwordid, purposeId, paragraphId);
            conditiondt.Columns.Add("TotalOrder", typeof(int));
            for (var i = 0; i < conditiondt.Rows.Count; i++)
            {
                conditiondt.Rows[i][conditiondt.Columns.Count - 1] = order++;
                dt.ImportRow(conditiondt.Rows[i]);
                GetCivilHeadwordDataAndOrder(conditiondt.Rows[i]["CC_ID"].ToString(), purposeId, paragraphId, dt);
            }
        }
        /// <summary>
        ///     得到相关节点，匹配挖空
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetRelLabel(string ccId)
        {
            DataTable conditiondt; //下级
            if (ReasonLabelDict.ContainsKey(ccId))
            {
                conditiondt = ReasonLabelDict[ccId];
            }
            else
            {
                conditiondt = xmlLabel_LabelRelBLL.GetList(ccId);
                ReasonLabelDict.Add(ccId, conditiondt);
            }
            return conditiondt;
        }
    }
}